﻿using MD5Hash;
using MODEL;

namespace DAL
{
    public class MemberDAL
    {
        public MemberDAL(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        //注册
        public int MemberAdd(MemberMODEL member)
        {
            try
            {
                var Exisit = Context.memberMODELs.Any(x => x.Card == member.Card);
                if (Exisit)
                {
                    return -1;
                }

                member.Pwd = member.Pwd.GetMD5();
                Context.memberMODELs.Add(member);
                return Context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        //登录
        public int MemberLogin(string card,string pwd)
        {
            try
            {
                var m = Context.memberMODELs.FirstOrDefault(x => x.Card == card);
                if (m == null)
                {
                    return -1;
                }
                else
                {
                    if (m.Pwd == pwd.GetMD5())
                    {
                        return 1;
                    }
                    else
                    {
                        return -2;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
